package com.hzh.util;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

public class ResponseUtil {
    
    private static final String  ENCODING_PREFIX  = "encoding";
    
    private static final String  NOCACHE_PREFIX   = "no-cache";
    
    private static final String  ENCODING_DEFAULT = "UTF-8";
    
    private static final boolean NOCACHE_DEFAULT  = true;
    
    public static void render(HttpServletResponse response, final String contentType,
        final String content, final String... headers) {
        try {
            // 分析headers参数
            String encoding = ENCODING_DEFAULT;
            boolean noCache = NOCACHE_DEFAULT;
            for (String header : headers) {
                String headerName = StringUtils.substringBefore(header, ":");
                String headerValue = StringUtils.substringAfter(header, ":");
                
                if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
                    encoding = headerValue;
                } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {
                    noCache = Boolean.parseBoolean(headerValue);
                } else {
                    throw new IllegalArgumentException(headerName + "不是一个合法的header类型");
                }
            }
            
            // 设置headers参数
            String fullContentType = contentType + ";charset=" + encoding;
            response.setContentType(fullContentType);
            if (noCache) {
                response.setHeader("Pragma", "No-cache");
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);
            }
            
            response.getWriter().write(content);
            response.getWriter().flush();
            
        } catch (IOException e) {
            e.printStackTrace();;
        }
    }
    
    public static void renderText(HttpServletResponse response, final String text,
        final String... headers) {
        render(response, "text/plain", text, headers);
    }
    
    public static void renderHtml(HttpServletResponse response, final String html,
        final String... headers) {
        render(response, "text/html", html, headers);
    }
    
    public static void renderXml(HttpServletResponse response, final String xml,
        final String... headers) {
        render(response, "text/xml", xml, headers);
    }
    
    public static void renderJson(HttpServletResponse response, final String string,
        final String... headers) {
        render(response, "application/json", string, headers);
    }
    
    @SuppressWarnings("rawtypes")
    public static void renderJson(HttpServletResponse response, final Map map,
        final String... headers) {
        String jsonString = JSON.toJSONString(map);
        renderJson(response, jsonString, headers);
    }
    
    public static void renderJson(HttpServletResponse response, final Object object,
        final String... headers) {
        String jsonString = JSON.toJSONString(object);
        renderJson(response, jsonString, headers);
    }
    
    public static void renderFile(HttpServletResponse response, String fileName, byte[] data)
        throws IOException {
        String fileNameUTF8 = URLEncoder.encode(fileName, "UTF-8");
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileNameUTF8 + "\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream;charset=UTF-8");
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        outputStream.write(data);
        outputStream.flush();
        outputStream.close();
    }
}
